package leaf.science.ui.jogwheel
{

import com.util.Geom;

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;

import leaf.science.Global;


[Embed(source="../library/JogWheel.swf", symbol="JogWheelThumb")]
public class JogWheelThumb extends MovieClip
{
	
	public var bg:Sprite;
	
	private var _isDrag:Boolean = false;
	private var _parent:JogWheel;
	
	public function JogWheelThumb()
	{
		buttonMode = true;
		mouseChildren = false;
		addEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
	}
	
	private function onAddedToStage( evt:Event ):void
	{
		addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
		stage.addEventListener( MouseEvent.MOUSE_UP, onStageMouseUp );
		stage.addEventListener( Event.MOUSE_LEAVE, onStageMouseUp );
		stage.addEventListener( MouseEvent.MOUSE_MOVE, onStageMouseMove );
		
		_parent = parent as JogWheel;
	}
	
	private function onStageMouseMove( evt:MouseEvent ):void
	{
		if( _isDrag )
		{
			var center:Point = JogWheel( parent ).center;
			var mouse:Point = new Point( evt.stageX, evt.stageY );
			var a:Number = Geom.angle( center, mouse );
			
			// move start angle 90 degrees ccw
			a = Geom.degrees( a );
			a = ( a-270 ) % 360;
			a = ( a<0 ? a+360 : a );
			
			_parent.value = a/360;
		}
	}
	
	private function onMouseDown( evt:MouseEvent ):void
	{
		_isDrag = true;
		if( stage )
			stage.mouseChildren = false;
	}

	private function onStageMouseUp( evt:Event ):void
	{
		_isDrag = false;
		if( stage )
			stage.mouseChildren = true;
	}
}
}